package com.lczyfz.istep.modules.openstack.api.neutron.impl;

import com.lczyfz.istep.modules.openstack.api.neutron.impl.parseJson.NeutronParse;
import com.lczyfz.istep.modules.openstack.api.neutron.interfaces.SubnetPools;
import com.lczyfz.istep.modules.openstack.api.neutron.model.SubnetPool;
import com.lczyfz.istep.modules.openstack.constvary.ConstParams;
import com.lczyfz.istep.modules.openstack.constvary.RequestType;
import com.lczyfz.istep.modules.openstack.utils.HTTP;
import com.lczyfz.istep.modules.openstack.utils.Request;
import com.lczyfz.istep.modules.openstack.utils.RequestFactory;
import com.lczyfz.istep.modules.openstack.utils.Response;
import net.sf.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * �����ؽӿڵ�ʵ����
 * @author clq
 *
 */
public class SubnetPoolImpl implements SubnetPools {
	public RequestFactory factory = new RequestFactory("neutron");
	
	@Override
	public List<SubnetPool> getSubnetPools() {
		List<SubnetPool> subnetpools = null;
		Request request = factory.getRequest();
		request.setUrl("/v2.0/subnetpools");
		request.setRequestType(RequestType.GET);
		
		Response results = HTTP.getConnectionResult(request);
		JSONObject body = results.getResponseBody();
		if(ConstParams.RESPONSE_CODE_NORMAL != results.getResponseCode()){
			throw new RuntimeException("Failed : HTTP error code : " + results.getResponseCode());
		}	
		
		subnetpools = NeutronParse.resolveSubnetPools(body);
		return subnetpools;
	}

	@Override
	public SubnetPool getSubnetPoolDetail(String subnetpoolId) {
		SubnetPool subnetpool = new SubnetPool();
		Request request = factory.getRequest();
		request.setUrl("/v2.0/subnetpools/" + subnetpoolId);
		request.setRequestType(RequestType.GET);
		
		Response results = HTTP.getConnectionResult(request);
		JSONObject body = results.getResponseBody();
		if(ConstParams.RESPONSE_CODE_NORMAL != results.getResponseCode()){
			throw new RuntimeException("Failed : HTTP error code : " + results.getResponseCode());
		}
		
		subnetpool = NeutronParse.resolveSubnetPoolDetail(body);
		return subnetpool;
	}

	@Override
	public SubnetPool createSubnetPool(SubnetPool spinput) {
		SubnetPool subnetpool = new SubnetPool();
		
		JSONObject params = NeutronParse.packageSubnetPool(spinput);
	//	System.out.println(params.toString());
		Request request = factory.getRequest();
		request.setUrl("/v2.0/subnetpools");
		request.setRequestType(RequestType.POST);
		request.setBody(params);
		
		Response results = HTTP.getConnectionResult(request);	
		if( ConstParams.RESPONSE_CODE_CREATED!= results.getResponseCode()){
			throw new RuntimeException("Failed : error code : " + results.getResponseCode());
		}
		JSONObject res = results.getResponseBody();
		
		subnetpool = NeutronParse.resolveCreateSubnetPool(res);
		return subnetpool;
	}

	@Override
	public void deleteSubnetPool(String subnetpoolId) {
		Request request = factory.getRequest();
		request.setUrl("/v2.0/subnetpools/" + subnetpoolId);
		request.setRequestType(RequestType.DELETE);
		
		Response results = HTTP.getConnectionResult(request);
		if(ConstParams.RESPONSE_CODE_SUCCESS  != results.getResponseCode() )
		{
			throw new RuntimeException("Failed : HTTP error code : " + results.getResponseCode());
		}
		return;
		
	}

	@Override
	public SubnetPool updateSubnetPool(String  subnetpoolId , SubnetPool spupdateinput) {
		SubnetPool subnetpool = new SubnetPool();
		
		JSONObject params = NeutronParse.packageSubnetPool(spupdateinput);
	//	System.out.println(params.toString());
		Request request = factory.getRequest();
		request.setUrl("/v2.0/subnetpools/" + subnetpoolId);
		request.setRequestType(RequestType.PUT);
		request.setBody(params);
		Response results = HTTP.getConnectionResult(request);	
		
		if( ConstParams.RESPONSE_CODE_NORMAL!= results.getResponseCode()){
			throw new RuntimeException("Failed : error code : " + results.getResponseCode());
		}
		JSONObject res = results.getResponseBody();
		
		subnetpool = NeutronParse.resolveUpdateSubnetPool(res);
		return subnetpool;
	}
	
	public boolean checkUniqueName(String spName){
		return true;
	}
	
	public static void main(String[] args){
		SubnetPoolImpl sptest = new SubnetPoolImpl();
//		System.out.println("��������Ϣ");
//		List<SubnetPool> splist = sptest.getSubnetPools();
//		for(SubnetPool sp : splist){
//			System.out.println(sp.getSpId()+" "+sp.getSpName() + " "+sp.getSpProjectId()+" "+sp.getSpTenantId()+" "+sp.getSpPrefixes().get(0)+" "+sp.getSpIsDefault()+"||"+
//					sp.getSpIpVersion()+" "+sp.getSpDefaultPrefixlen());
//		}
		
//		System.out.println("��������ϸ��Ϣ");
//		SubnetPool sp = sptest.getSubnetPoolDetail("9f3156cf-b734-46d8-91ad-84a787fd41dc");
//		System.out.println(sp.getSpId()+" "+sp.getSpName() + " "+sp.getSpProjectId()+" "+sp.getSpTenantId()+" "+sp.getSpPrefixes()+" "+sp.getSpIsDefault()+" "+sp.getSpUpdatedAt()+" "+sp.getSpCreatedAt()+"||"+
//				sp.getSpIpVersion()+" "+sp.getSpDefaultPrefixlen());
//		
		System.out.println("����������");
		ArrayList<String> subnetprefix = new ArrayList<String>();
		subnetprefix.add("172.168.0.0/16");
		SubnetPool sptt = new SubnetPool();
		sptt.setSpName("sp4");
		sptt.setSpPrefixes(subnetprefix);
		sptt.setSpDefaultPrefixlen("16");
		SubnetPool sp_new = sptest.createSubnetPool(sptt);
		System.out.println(sp_new.getSpId()+" "+sp_new.getSpName() + " "+sp_new.getSpProjectId()+" "+sp_new.getSpTenantId()+" "+sp_new.getSpPrefixes().get(0)+" "+sp_new.getSpIsDefault());
	
	//	System.out.println("ɾ��������");
	//	sptest.deleteSubnetPool("2766655a-7f8d-4de3-82db-af948b415c50");
		
	/*	System.out.println("����������");
		ArrayList<String> subnetprefix = new ArrayList<String>();
		subnetprefix.add("172.168.0.2/16");
		SubnetPool spttupdate = new SubnetPool();
		spttupdate.setSpPrefixes(subnetprefix);
		spttupdate.setSpDefaultPrefixlen("16");
		SubnetPool sp_update = sptest.updateSubnetPool("2766655a-7f8d-4de3-82db-af948b415c50", spttupdate);
		System.out.println(sp_update.getSpId()+" "+sp_update.getSpName() +" "+sp_update.getSpTenantId()+" "+sp_update.getSpPrefixes().get(0)+" "+sp_update.getSpIsDefault());
	*/
	}
}
